pipeline{
  agent any
//   agent {
//       label 'maven'
//     }
  stages {
    stage('checkout source code') {
        steps {
            echo '1.checkout source code stage'
            checkout(
                [$class: 'GitSCM',
                branches: [[name: '*/develop']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [],
                submoduleCfg: [],
                userRemoteConfigs: [
                    [
                    url: 'git@10.170.33.107:oscoe/guadalcanal/template/gua-dev-starter.git']
                    ]
                ]
            )
        }
    }

    stage('echo commit id') {
        steps {
            script {
             env.imageTag = sh (script: 'git rev-parse --short HEAD ${GIT_COMMIT}', returnStdout: true).trim()
             echo "${imageTag}"
            }
        }
    }
    stage('npm build') {
        steps {
           echo '2.npm build stage'
           nodejs('nodejs16.13.0') {
             sh label: '', script: '''
             rm -f package-lock.json
             rm -f pnpm-lock.yaml
             pnpm add rollup@">=2.50.0 <3.0.0"
             pnpm install --registry http://oscoe.neusoft.com:8081/repository/npm/ 
             pnpm build  '''
           }
        }
    }

    stage('build image') {
        steps {
              script {
                  echo '5.build image'
                  appname="gua-dev-starter"
                  tag="${imageTag}"
                  //根据项目信息替换模板内容
                  sh "docker build -t 10.170.33.110/guadalcanal/${appname}:dev-${imageTag} $WORKSPACE/dist/"
                  sh "docker tag  10.170.33.110/guadalcanal/${appname}:dev-${imageTag} 10.170.33.110/guadalcanal/${appname}:latest " 
                  sh "docker push 10.170.33.110/guadalcanal/${appname}:dev-${imageTag}"
	              sh "docker push 10.170.33.110/guadalcanal/${appname}:latest"
                  sh "docker rmi 10.170.33.110/guadalcanal/${appname}:dev-${imageTag}"
            }
        }
    }

    stage('deploy') {
        steps {
            script {
                echo '6.deploy' 
                sh "helm upgrade --install gua-dev-starter /osmium/helm/gua-dev-starter/  --kubeconfig=/root/.kube/120-config/config  "
            }
        }
    }
  }
    post {
       always {
        emailext body: '''<hr/>
        (本邮件是程序自动下发的，请勿回复！)<br/><hr/>
        项目名称：$PROJECT_NAME<br/><hr/>
        构建编号：$imageTag<br/><hr/>
        构建状态：$BUILD_STATUS<br/><hr/>
        触发原因：${CAUSE}<br/><hr/>
        构建日志地址：<a href="${BUILD_URL}console">${BUILD_URL}console</a><br/><hr/>
        构建地址：<a href="$BUILD_URL">$BUILD_URL</a><br/><hr/>
        变更集:${JELLY_SCRIPT,template="html"}<br/><hr/>''', subject: '$PROJECT_NAME - Build # $imageTag - $BUILD_STATUS!', to: 'wei-chl@neusoft.com'
       } 
    }
}